﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Fcdbas.WinUI.Forms;

using Fcdbas.Platform.Interface;
using Fcdbas.WinUI.Common;
using Fcdbas.Core.Windows;
using Fcdbas.WinUI.Windows;
using Fcdbas.Platform.Properties;
using Fcdbas.Platform;
using Fcdbas.WinUI.Controls;
using Fcdbas.Core;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;



namespace Fcdbas.WinUI.Forms
{
    public partial class dlgGridSummarySet : cDialogWindow
    {
        private cGridControl grid;
        public dlgGridSummarySet(cGridControl gd)
        {
            InitializeComponent();
            grid = gd;
        }

        private void dlgGridSummarySet_Load(object sender, EventArgs e)
        {
            Init();
            cboSummaryType.Enabled = true;
            txtDisplayFormat.Enabled = true;
            if (currentGridView.OptionsView.ShowFooter)
                btnList.Text = "隐藏统计";
            else
                btnList.Text = "显示统计";
        //    gc.SummaryItem.SummaryType
        }
        private void Init()
        {
            foreach (DevExpress.Data.SummaryItemType item in Enum.GetValues(typeof(DevExpress.Data.SummaryItemType)))
            {
                string caption = "没有";
                if (item == DevExpress.Data.SummaryItemType.Average)
                    caption = "平均";
                if (item == DevExpress.Data.SummaryItemType.Count)
                    caption = "计数";
                if (item == DevExpress.Data.SummaryItemType.Custom)
                    caption = "自定义";
                if (item == DevExpress.Data.SummaryItemType.Max)
                    caption = "最大";
                if (item == DevExpress.Data.SummaryItemType.Min)
                    caption = "最小";
                if (item == DevExpress.Data.SummaryItemType.Sum)
                    caption = "合计";
                cboSummaryType.AddItem(item, caption);
            }
            if (grid == null)
                return;
            currentGridView = grid.MainView as GridView;
            if (currentGridView != null)
            {
                foreach (GridColumn gc in currentGridView.Columns)
                {
                    lbColumns.Items.Add(gc.Name + "<" + gc.Caption + ">");
                }
            }
            lbColumns.SelectedIndex = 0;
        }
        private void BindToUI(GridColumn gc)
        {
            cboSummaryType.SetValue(gc.SummaryItem.SummaryType);
            txtDisplayFormat.Text  = gc.SummaryItem.DisplayFormat;
            txtFieldName.Text = gc.SummaryItem.FieldName;
        }
        private GridColumn currentGC = null;
        private GridView currentGridView = null;
        private void lbColumns_SelectedIndexChanged(object sender, EventArgs e)
        {
            string item = lbColumns.SelectedItem.ToString();
            GridView view = grid.MainView as GridView;
            if (view != null)
            {
                foreach (GridColumn gc in view.Columns)
                {
                    if (gc.Name + "<" + gc.Caption + ">" == item)
                    {
                        currentGC = gc;
                        BindToUI(gc);
                    }
                }
            }
        }
        private void UIToObject(GridColumn gc)
        {
            if (currentGC != null)
            {
                gc.SummaryItem.SummaryType = (DevExpress.Data.SummaryItemType)cboSummaryType.GetCurrentItem();
                gc.SummaryItem.DisplayFormat = txtDisplayFormat.Text;
                if (gc.Summary.Count > 0)
                    gc.Summary[0].SummaryType = gc.SummaryItem.SummaryType;
                GridSummaryItem oldItem = null;
                foreach(GridSummaryItem item in currentGridView.GroupSummary)
                {
                    if(item.FieldName == gc.SummaryItem.FieldName)
                        oldItem = item;
                }
                if (gc.SummaryItem.SummaryType == DevExpress.Data.SummaryItemType.None)
                {
                    if (oldItem != null)
                        currentGridView.GroupSummary.Remove(oldItem);
                }
                else
                {
                    if (oldItem == null)
                    {
                        currentGridView.GroupSummary.Add(gc.SummaryItem.SummaryType, gc.SummaryItem.FieldName, gc, gc.SummaryItem.DisplayFormat);
                    }
                    else
                    {
                       // oldItem = new GridColumnSummaryItem();
                        oldItem.FieldName = gc.SummaryItem.FieldName;
                        oldItem.DisplayFormat = gc.SummaryItem.DisplayFormat;
                        oldItem.SummaryType = gc.SummaryItem.SummaryType;
                     //   currentGridView.GroupSummary.Add(oldItem);
                    }
                }
            }
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            UIToObject(currentGC);
        }

        private void cboSummaryType_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBoxData cd = cboSummaryType.SelectedItem as ComboBoxData;
            if (cd != null)
            {
                DevExpress.Data.SummaryItemType itemType = (DevExpress.Data.SummaryItemType)cd.Value;
                if (itemType == DevExpress.Data.SummaryItemType.Count)
                {
                    txtDisplayFormat.Text = cd.Text + "={0:n}";
                }
                else if (itemType == DevExpress.Data.SummaryItemType.Average)
                {
                    txtDisplayFormat.Text = cd.Text + "={0:f}";
                }
                else if (itemType == DevExpress.Data.SummaryItemType.Sum)
                {
                    txtDisplayFormat.Text = cd.Text + "={0:f}";
                }
                else
                    txtDisplayFormat.Text = cd.Text + "={0}";

            }
        }

        private void btnList_Click(object sender, EventArgs e)
        {
            if (!currentGridView.OptionsView.ShowFooter)
            {
                btnList.Text = "隐藏统计";
                currentGridView.OptionsView.ShowFooter = true;
            }
            else
            {
                btnList.Text = "显示统计";
                currentGridView.OptionsView.ShowFooter = false;
            }
        }
    }
}
